package com.lrkj.back.controller;

import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.StartUpOnLoadData;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lrkj.back.entity.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.service.TbDepartmentService;
import com.lrkj.back.service.TbDictService;
import com.lrkj.back.service.TbEmployeeService;
import com.lrkj.back.service.TbPackageMubanService;
import com.lrkj.back.service.TbProductService;
import com.lrkj.back.service.TbSeqService;
import com.lrkj.back.service.ZsEquipCeshiService;
import com.lrkj.back.service.ZsEquipService;
import com.lrkj.back.service.ZsMIejunMingxiService;
import com.lrkj.back.service.ZsMiejunBuhegeService;
import com.lrkj.back.service.ZsMiejunService;
import com.lrkj.back.service.ZsPackageImformationService;
import com.lrkj.back.service.ZsPeibaoService;
import com.lrkj.back.service.ZsSoftService;
import com.lrkj.back.service.ZsWanglanService;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/miejun")
public class ZsMiejunController extends BaseController {

    @Autowired
    private TbSeqService seqService;

    @Autowired
    private ZsSoftService zsSoftService;

    @Autowired
    private ZsMiejunService miejunService;

    @Autowired
    private ZsEquipService zsEquipService;

    @Autowired
    private ZsWanglanService zsWanglanService;

    @Autowired
    private ZsMIejunMingxiService zsMiejunMingxiService;

    @Autowired
    private ZsPeibaoService zsPeibaoService;

    @Autowired
    private ZsEquipCeshiService zsEquipCeshiService;

    @Autowired
    private ZsMiejunBuhegeService zsMiejunBuhegeService;

    @Autowired
    private ZsPackageImformationService zsPackageInformationService;

    @Autowired
    private TbPackageMubanService tbPackageMubanService;

    @Autowired
    private TbEmployeeService tbEmployeeService;

    @Autowired
    private TbDictService tbDictService;

    @Autowired
    private TbProductService tbProductService;

    @Autowired
    private ZsMiejunBuhegeService miejunbuhegeService;

    @Autowired
    private TbDepartmentService departmentService;

    @Autowired
    private ZsEquipService equipService;
    @Autowired
    private ZsEquipCeshiService equipCeshiSercie;

    @Autowired
    private ZsWupingfafangController zsWupingfafangController;

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/miejuning.do")
    public ModelAndView miejunIng(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        map.put("miejunZhuangtai", 1);
        map.put("miejunHege", 0);
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        map.put("miejunjieshu", "miejunjieshu");
        PageInfo<ZsMiejun> page = miejunService.findAllMiejun(map);
        request.setAttribute("lstMiejun", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        return new ModelAndView("/mj/miejunguocheng/miejun_gc");
    }

    @RequestMapping("/miejunListAndroid")
    @ResponseBody
    public Map<String, Object> miejunAndroid(Integer type)
            throws BusinessException {
//        Map<String,Object> rtnMap = new HashMap<>();
//        Map<String, Object> map = new HashMap<>();
//        map.put("peibaoZhuangtai", 1);
//        map.put("peibaoHege", 1);
//        map.put("mjLiushui", "mjLiushui");
//        map.put("changjia", "changjia");
//        map.put("peibaoren", "peibaoren");
//        List<ZsPackageImformation> packageImformations = new ArrayList<>();
//        packageImformations = zsPackageInformationService.findAll(map);
//        rtnMap.put("datas",packageImformations);

        Map<String,Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
//        map.put("peibaoZhuangtai", 1);
        map.put("peibaoHege", 1);
        map.put("mjLiushui", "mjLiushui");
        map.put("changjia", "changjia");
        map.put("peibaoren", "peibaoren");
        List<ZsPackageImformation> packageImformations = zsPackageInformationService.findAll(map);
        Iterator<ZsPackageImformation> iterator = packageImformations.iterator();
        while (iterator.hasNext()){
            ZsPackageImformation zsPackageImformation = iterator.next();
            TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
            if(tbPackageMuban !=null){
                int print = tbPackageMuban.getPackagePrint();
                if(print==0){
                    iterator.remove();
                }
            }else{
                iterator.remove();
            }
        }
        rtnMap.put("datas",packageImformations);
        rtnMap.put("status",true);
        rtnMap.put("dictmap",StartUpOnLoadData.singleDataMap);
        return rtnMap;
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/miejunend.do")
    @ResponseBody
    public Map<Object,Object> miejunEnd(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        ZsMiejun zsMiejun = new ZsMiejun();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<Object, Object> rtnMap = new HashMap<>();
        Date date = new Date();
        try {
            List<ZsPackageImformation> packageImformations = new ArrayList<>();
            String[] miejunLiushui = request.getParameter("id").split(",");
            for (int i = 0; i < miejunLiushui.length; i++) {
                zsMiejun.setMiejunZhuangtai(2);
                zsMiejun.setMiejunJieshuTime(date);
                zsMiejun.setRecordTime(date);
                zsMiejun.setMiejunLiushui(miejunLiushui[i]);
                miejunService.updateByPrimaryKey(zsMiejun);
                map1.put("recordTime", date);
                map1.put("miejunLiushui", miejunLiushui[i]);
                zsMiejunMingxiService.updateEndTimeAndHege(map1);
                map2.put("miejunLiushui", miejunLiushui[i]);
                List<ZsMiejunMingxi> miejunMingxiList = zsMiejunMingxiService.findWaitShenhe(map2);
                for (ZsMiejunMingxi zsMiejunMingxi : miejunMingxiList) {
                    String packageTiaoma = zsMiejunMingxi.getMiejunWupinbaoTiaoma();
                    ZsPackageImformation zsPackageImformation = new ZsPackageImformation();
                    zsPackageImformation.setMiejunJieshuTime(date);
                    zsPackageImformation.setMiejunZhuangtai(2);
                    zsPackageImformation.setPackageTiaoma(packageTiaoma);
                    packageImformations.add(zsPackageImformation);
                }
            }
            if(packageImformations.size()!=0){
                zsPackageInformationService.updateByBatch(packageImformations);
            }

            rtnMap.put("state", "true");
            rtnMap.put("msg", "成功");
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * @param miejunLiushui
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/miejunEdit.do")
    public ModelAndView miejunEdit(String miejunLiushui, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        ZsMiejun zsMiejun = miejunService.selectByPrimaryKey(miejunLiushui);
        request.setAttribute("miejun", zsMiejun);
        return new ModelAndView("/mj/miejunguocheng/miejun_edit");
    }

    /**
     * @param
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/miejunEditDo.do")
    @ResponseBody
    public Map<Object, Object> miejunEditDo(ZsMiejun zsMiejun, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        ZsMiejun zsMiejun2 = new ZsMiejun();
        try {
            zsMiejun2.setMiejunShebeiTiaoma(zsMiejun.getMiejunShebeiTiaoma());
            zsMiejun2.setMiejunRuanjianTiaoma(zsMiejun.getMiejunRuanjianTiaoma());
            zsMiejun2.setMiejunMeijunrenTiaoma(zsMiejun.getMiejunMeijunrenTiaoma());
            zsMiejun2.setMiejunLiushui(zsMiejun.getMiejunLiushui());
            int i = miejunService.updateByPrimaryKey(zsMiejun2);
            if (i == 1) {
                rtnMap.put("state", "true");
                rtnMap.put("msg", "成功");
            } else {
                rtnMap.put("state", "false");
                rtnMap.put("msg", "失败");
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/miejunDel.do")
    @ResponseBody
    public Map<Object, Object> miejunDel(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        try {
            String ids = request.getParameter("id");
            String[] arraId = ids.split(",");
            for (String tid : arraId) {
                miejunService.deleteByPrimaryKey(tid);
                zsMiejunMingxiService.deleteByPrimaryKey(tid);
            }
            rtnMap.put("state", "true");
            rtnMap.put("msg", "成功");
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * 要根据包条码进行判断

     */
    //灭菌--化学检测审核   点击保存
    @RequestMapping("/updateShenheAndHege.do")
    @ResponseBody
    public Map<Object, Object> updateHege(String pdata,String[] miejunLiushui, Integer[] miejunHege, String miejunShenherenTiaoma
            ) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        List<ZsPackageImformation> packageImformations = new ArrayList<>();
        Date date = new Date();
        List<ZsMiejun> miejuns = new ArrayList<>();
        List<ZsMiejunBuhege> buheges = new ArrayList<>();
        if(pdata!=null&&!"".equals(pdata)){
            JSONObject jsonObject = JSONObject.parseObject(pdata);
            JSONArray jsonArray = jsonObject.getJSONArray("datas");
            miejunShenherenTiaoma = jsonObject.getString("peoplecode");
            miejunLiushui = new String [jsonArray.size()];
            miejunHege = new Integer [jsonArray.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                miejunLiushui[i] =  JSONObject.parseObject(jsonArray.get(i).toString()).getString("miejunLiushui");
                miejunHege[i] = Integer.valueOf(JSONObject.parseObject(jsonArray.get(i).toString()).getString("miejunhege"));
            }
        }
        miejunShenherenTiaoma = miejunShenherenTiaoma.toUpperCase();
        TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(miejunShenherenTiaoma);
        try {
            Integer hege;
            Long[] seqVal = seqService.seqValBatch("miejunbuhegeno", miejunLiushui.length);
            ZsPackageImformation packageIm = null;
            for (int i = 0; i < miejunLiushui.length; i++) {
                hege = miejunHege[i];
                //合格一次的信息
                if (hege == 2) {// 合格
                    map.put("miejunLiushui", miejunLiushui[i]);
//                    List<ZsMiejunMingxi> list = zsMiejunMingxiService.findWaitShenhe(map);

                    List<ZsPackageImformation> zsPackageImformations = zsPackageInformationService.findAll(map);
                    if(zsPackageImformations.size() !=0){
                        for (ZsPackageImformation packageImformation : zsPackageImformations) {
                            TbPackageMuban tbPackageMuban = null;
                            packageIm = packageImformation;
                            Integer packageMiejunNo = 3;
                            if (packageIm != null) {
                                tbPackageMuban = packageIm.getTbPackageMuban();
                                if (tbPackageMuban != null) {
                                    packageMiejunNo = tbPackageMuban.getPackageMiejunNo();
                                }
                            }
                            if (packageMiejunNo == 2) {// 需进行二次审核
                                ZsMiejun miejun = new ZsMiejun();
                                miejun.setMiejunShenherenTiaoma(miejunShenherenTiaoma.toUpperCase());
                                miejun.setMiejunHege(2);
                                miejun.setMiejunShenheTime(date);
                                miejun.setMiejunLiushui(miejunLiushui[i]);
                                miejuns.add(miejun);
//                            zsMiejunMingxiService.updateEndTimeAndHege(map1);
                                packageImformation.setMiejunShenheTime(date);
                                packageImformation.setMiejunHege(2);
                                if (tbEmployee != null) {
                                    packageImformation.setMiejunShenherenTiaoma(miejunShenherenTiaoma.toUpperCase());
                                    packageImformations.add(packageImformation);

                                }
                            } else {// 一次审核就行
                                ZsMiejun miejun = new ZsMiejun();
                                miejun.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                                miejun.setMiejunHege(3);
                                miejun.setMiejunShenheTime(date);
                                miejun.setMiejunLiushui(miejunLiushui[i]);
                                miejun.setRecordTime(date);
                                miejuns.add(miejun);
                                Map<String,Object> map1 = new HashMap<>();
                                map1.put("miejunShenheTime", date);
                                map1.put("miejunHege", 2);
                                map1.put("miejunShenherenTiaoma", miejunShenherenTiaoma);
                                map1.put("recordTime", date);
                                map1.put("miejunLiushui", miejunLiushui[i]);
                                map1.put("miejunWupinbaoTiaoma", packageImformation.getPackageTiaoma());
                                zsMiejunMingxiService.updateEndTimeAndHege(map1);
                                packageImformation.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                                packageImformation.setMiejunShenheTime(date);
                                packageImformation.setMiejunHege(3);
                                if (tbEmployee != null) {
                                    // String miejunshenherenName =
                                    // tbEmployee.getEmployeeName();
                                    // packageImformation.setMiejunMiejunrenName(miejunshenherenName);
                                    if (tbEmployee.getEmployeeNo().equals(miejunShenherenTiaoma)) {
                                        packageImformations.add(packageImformation);
                                    }
                                }
                            }
                        }
                    }else{
                        ZsMiejun miejun = new ZsMiejun();
                        miejun.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                        miejun.setMiejunHege(3);
                        miejun.setMiejunShenheTime(date);
                        miejun.setMiejunLiushui(miejunLiushui[i]);
                        miejun.setRecordTime(date);
                        miejuns.add(miejun);
                    }

                } else {// 不合格
                    ZsMiejunBuhege miejunbuhege = null;
                    ZsMiejun miejun = new ZsMiejun();
                    miejun.setMiejunHege(0);
                    miejun.setMiejunZhuangtai(0);
                    miejun.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                    miejun.setMiejunShenheTime(date);
                    miejun.setMiejunLiushui(miejunLiushui[i]);
                    miejun.setRecordTime(date);
                    miejuns.add(miejun);
                    map.put("miejunLiushui", miejunLiushui[i]);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
                    List<ZsMiejunMingxi> list = zsMiejunMingxiService.findWaitShenhe(map);
                    for (ZsMiejunMingxi zsMiejunMingxi : list) {
                        miejunbuhege = new ZsMiejunBuhege();
                        String packageTiaoma = zsMiejunMingxi.getMiejunWupinbaoTiaoma();
                        Map<String, Object> map1 = new HashMap<>();
                        map1.put("miejunShenheTime", date);
                        map1.put("miejunHege", hege);
                        map1.put("miejunShenherenTiaoma", miejunShenherenTiaoma);
                        map1.put("recordTime", date);
                        map1.put("miejunLiushui", miejunLiushui[i]);
                        map1.put("miejunWupinbaoTiaoma", packageTiaoma);
//                        zsMiejunMingxiService.updateEndTimeAndHege(map1);
                        ZsPackageImformation packageImformation = new ZsPackageImformation();
                        packageImformation.setMiejunZhuangtai(0);
                        packageImformation.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                        packageImformation.setMiejunShenheTime(date);
                        packageImformation.setMiejunHege(0);
                        packageImformation.setPackageTiaoma(packageTiaoma);
                        if (tbEmployee != null) {
                            if (tbEmployee.getEmployeeNo().equals(miejunShenherenTiaoma)) {
                                packageImformations.add(packageImformation);
                            }
                        }
                    }
                    String miejunbuhegeLiushui = sdf.format(new Date()) + seqVal[i];
                    miejunbuhege.setMiejunBuhegeLiushui(miejunbuhegeLiushui);
                    miejunbuhege.setMiejunLiushui(miejunLiushui[i]);
                    miejunbuhege.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                    miejunbuhege.setMiejunHege(hege);
                    miejunbuhege.setMiejunShenheTime(date);
                    miejunbuhege.setRecordTime(date);
                    buheges.add(miejunbuhege);
                }
            }
            if (!buheges.isEmpty()) {
                miejunbuhegeService.insertBatch(buheges);
            }
            if (packageImformations.size() != 0) {
                zsPackageInformationService.updateByBatch(packageImformations);
                List<String> packageTiaoma = new ArrayList<>();
                for (int i = 0; i < packageImformations.size(); i++) {
                    ZsPackageImformation zsPackageImformations = packageImformations.get(i);
                    TbPackageMuban tbPackageMuban = zsPackageImformations.getTbPackageMuban();
                    if(tbPackageMuban!=null){
                        //这种常量太坑了 罪过罪过
                        if(tbPackageMuban.getPackageDepartid()==15){
                            packageTiaoma.add(zsPackageImformations.getPackageTiaoma());
                        }
                    }

                }
                String [] packageTiaomas = packageTiaoma.toArray(new String[packageTiaoma.size()]);
                if(packageTiaomas.length!=0){
                    zsWupingfafangController.piliangfafang(miejunShenherenTiaoma,packageTiaomas);
                }
                rtnMap.put("msg", "审核成功");
                rtnMap.put("state", "true");
            } else {
//                rtnMap.put("state", "false");
//                rtnMap.put("msg", "无效审核人条码");
            }
            miejunService.updateByBatch(miejuns);
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "审核失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * 第二次进行审核
     * 
     * @param miejunLiushui
     * @param miejunHege
     * @param miejunShenherenTiaoma
     * @param request
     * @param response
     */
    //灭菌--生物监测审核   点击保存
    @RequestMapping("/updateShenheAndHegeTwo.do")
    @ResponseBody
    public Map<Object, Object> updateHegeTwo(String[] miejunLiushui, Integer[] miejunHege, String miejunShenherenTiaoma,
            HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        List<ZsPackageImformation> packageImformations = new ArrayList<>();
        Date date = new Date();
        TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(miejunShenherenTiaoma);
        List<ZsMiejun> miejuns = new ArrayList<>();
        List<ZsMiejunBuhege> buheges = new ArrayList<>();
        try {
            Integer hege;
            Long[] seqVal = seqService.seqValBatch("miejunbuhegeno", miejunLiushui.length);
            for (int i = 0; i < miejunLiushui.length; i++) {
                hege = miejunHege[i];


                if (hege == 2) {// 合格
                    map.put("miejunLiushui", miejunLiushui[i]);
                    List<ZsMiejunMingxi> list = zsMiejunMingxiService.findWaitShenhe(map);
                    List<ZsPackageImformation> zsPackageImformations = zsPackageInformationService.findAll(map);
                    for (ZsPackageImformation packageImformation : zsPackageImformations) {
                        TbPackageMuban tbPackageMuban = packageImformation.getTbPackageMuban();

                        Integer packageMiejunNo = 3;
                        if(packageImformation!=null){
                            if(tbPackageMuban !=null){
                                packageMiejunNo = tbPackageMuban.getPackageMiejunNo();
                            }
                        }
                        if (packageMiejunNo == 2) {// 需进行二次审核
                            // 需要进行二次灭菌的包
                            ZsMiejun miejun = new ZsMiejun();
                            miejun.setMiejunShenherenTiaoma(miejunShenherenTiaoma.toUpperCase());
                            // miejun.setMiejunHege(3);
                            miejun.setMiejunShenheTime(date);
                            miejun.setMiejunLiushui(miejunLiushui[i]);
                            miejun.setRecordTime(date);
                            miejuns.add(miejun);
//
//                            Map<String, Object> map1 = new HashMap<String, Object>();
//                            map1.put("miejunShenheTime", date);
//                            map1.put("miejunHege", 3);
//                            map1.put("miejunShenherenTiaoma", miejunShenherenTiaoma);
//                            map1.put("recordTime", date);
//                            zsMiejunMingxiService.updateEndTimeAndHege(map1);

                            packageImformation.setMiejunShenherenTiaoma(miejunShenherenTiaoma.toUpperCase());
                            packageImformation.setMiejunShenheTime(date);
                            packageImformation.setMiejunHege(3);
                            if (tbEmployee != null) {
                                packageImformations.add(packageImformation);
                                if (tbEmployee.getEmployeeNo().equals(miejunShenherenTiaoma)) {

                                }
                            }
                        }
                    }
                } else {// 不合格
                    map.put("miejunLiushui", miejunLiushui[i]);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
                    List<ZsMiejunMingxi> list = zsMiejunMingxiService.findWaitShenhe(map);

                    ZsMiejunBuhege miejunbuhege = null;
                    for (ZsMiejunMingxi zsMiejunMingxi : list) {
                        miejunbuhege = new ZsMiejunBuhege();
                        String packageTiaoma = zsMiejunMingxi.getMiejunWupinbaoTiaoma();
                        ZsMiejun miejun = new ZsMiejun();
                        // miejun.setMiejunHege(hege);
                        miejun.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                        miejun.setMiejunShenheTime(date);
                        miejun.setMiejunLiushui(miejunLiushui[i]);
                        miejun.setRecordTime(date);
                        miejuns.add(miejun);

                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map1.put("miejunShenheTime", date);
                        map1.put("miejunHege", hege);// 1不合格
                        map1.put("miejunShenherenTiaoma", miejunShenherenTiaoma);
                        map1.put("recordTime", date);
                        map1.put("miejunLiushui", miejunLiushui[i]);
                        zsMiejunMingxiService.updateEndTimeAndHege(map1);

                        ZsPackageImformation packageImformation = new ZsPackageImformation();
                        packageImformation.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                        packageImformation.setMiejunShenheTime(date);
                        packageImformation.setMiejunHege(hege);// 1不合格
                        packageImformation.setPackageTiaoma(packageTiaoma);
                        if (tbEmployee != null) {
                            if (tbEmployee.getEmployeeNo().equals(miejunShenherenTiaoma)) {
                                packageImformations.add(packageImformation);
                            }
                        }
                    }
                    String miejunbuhegeLiushui = sdf.format(new Date()) + seqVal[i];
                    miejunbuhege.setMiejunBuhegeLiushui(miejunbuhegeLiushui);
                    miejunbuhege.setMiejunLiushui(miejunLiushui[i]);
                    miejunbuhege.setMiejunShenherenTiaoma(miejunShenherenTiaoma);
                    miejunbuhege.setMiejunHege(hege);
                    miejunbuhege.setMiejunShenheTime(date);
                    miejunbuhege.setRecordTime(date);
                    buheges.add(miejunbuhege);
                }
            }
            if (!buheges.isEmpty()) {
                miejunbuhegeService.insertBatch(buheges);
            }
            if (packageImformations.size() != 0) {
                zsPackageInformationService.updateByBatch(packageImformations);
                miejunService.updateByBatch(miejuns);
                List<String> packageTiaoma = new ArrayList<>();
                for (int i = 0; i < packageImformations.size(); i++) {
                    ZsPackageImformation zsPackageImformations = packageImformations.get(i);
                    TbPackageMuban tbPackageMuban = zsPackageImformations.getTbPackageMuban();
                    //这种常量太坑了 罪过罪过
                    if(tbPackageMuban!=null){
                        if(tbPackageMuban.getPackageDepartid()==15){
                            packageTiaoma.add(zsPackageImformations.getPackageTiaoma());
                        }
                    }

                }
                String [] packageTiaomas = packageTiaoma.toArray(new String[packageTiaoma.size()]);
                if(packageTiaomas.length!=0){
                    zsWupingfafangController.piliangfafang(miejunShenherenTiaoma,packageTiaomas);
                }
                rtnMap.put("state", "true");
                rtnMap.put("msg", "审核成功");
            } else {
                rtnMap.put("state", "false");
                rtnMap.put("msg", "不用进行二次审核或无效审核人条码");
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "审核失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * 查找所有的灭菌合格的包的灭菌信息（灭菌清单查询）
     * @param searchTxt
     *                  输入框搜索条件
     * @param request
     * @param response
     * @return
     * @throws ParseException
     */
    //灭菌--清单查询    灭菌明细表
    @RequestMapping("/miejunSHjieguo.do")
    public ModelAndView miejunSHjieguo(String shijian, String searchTxt, String tiaojian, HttpServletRequest request,
            HttpServletResponse response) throws BusinessException, ParseException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<String, Object>();
        String st = request.getParameter("startTime");
        String et = request.getParameter("endTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        Map<String, Object> tiaojianMap = new HashMap<>();
        List<String> tiaomaList = new ArrayList<>();
        List<String> intList = new ArrayList<>();
        if (StringUtils.isNotBlank(tiaojian)) {
            tiaojianMap.put("tiaojian", tiaojian);
            List<ZsEquip> equips = zsEquipService.findEquipListByType(tiaojianMap);
            List<ZsMiejun> miejun = miejunService.findAllMiejunNoPage(tiaojianMap);
            List<TbEmployee> employee = tbEmployeeService.findEmployee(tiaojianMap);
            List<TbPackageMuban> packageMuban = tbPackageMubanService.findPackage(tiaojianMap);
            // 判断有无此设备
            if (!equips.isEmpty()) {
                for (ZsEquip zsEquip : equips) {
                    if (!tiaomaList.contains(zsEquip.getEquipId())) {
                        tiaomaList.add(zsEquip.getEquipId().toString());
                    }
                }
                map.put("miejunShebeiTiaomaArr", tiaomaList);
            }
            // 判断有无此科室
            if (!miejun.isEmpty()) {
                for (ZsMiejun mj : miejun) {
                    if (!tiaomaList.contains(mj.getMiejunLiushui())) {
                        tiaomaList.add(mj.getMiejunLiushui());
                    }
                }
                map.put("miejunliushuiArr", tiaomaList);
                // map.put("miejunShebeiTiaoma", department.getDepartmentId());
            }
            // 判断有无此员工
            if (!employee.isEmpty()) {
                for (TbEmployee tbEmployee : employee) {
                    if (!tiaomaList.contains(tbEmployee.getEmployeeNo())) {
                        tiaomaList.add(tbEmployee.getEmployeeNo());
                    }
                }
                map.put("ren", "miejun");
                map.put("renArr", tiaomaList);
            }
            // 判断有无此包模板
            if (!packageMuban.isEmpty()) {
                for (TbPackageMuban tbPackageMuban : packageMuban) {
                    if (!intList.contains(tbPackageMuban.getPackageId())) {
                        intList.add(tbPackageMuban.getPackageId());
                    }
                }
                map.put("packageIdArr", intList);
            }
            if (equips.isEmpty() && miejun.isEmpty() && employee.isEmpty() && packageMuban.isEmpty()) {
                map.put("miejunShebeiTiaomaArr", "0");
                map.put("miejunMeijunrenTiaoma", "0");
            }
        }
        if (StringUtils.isNotBlank(st)) {
            startTime = sdf.parse(st);
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(et)) {
            endTime = sdf.parse(et);
            map.put("endTime", endTime);
        }
        if (StringUtils.isBlank(st) && StringUtils.isBlank(et)) {
            map.put("startTime", lastDayWholePointDate(new Date()));
            map.put("endTime", new Date());
        }
        map.put("chaxun", "miejun");
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        map.put("miejunHege", 3);
        map.put("miejunLiushui", "miejunliushui");
        map.put("miejunqingdan", "miejunqingdan");
        PageInfo<ZsPackageImformation> page = zsPackageInformationService.tongjiChaxunPage(map);
        if (!page.getList().isEmpty()) {
            request.setAttribute("packageinformationList", page.getList());
            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
            request.setAttribute("total", page.getPages());
            request.setAttribute("equipMap", StartUpOnLoadData.equipMap);
            request.setAttribute("softMap", StartUpOnLoadData.softMap);
            request.setAttribute("dictMap", StartUpOnLoadData.singleDataMap);
            request.setAttribute("emMap", StartUpOnLoadData.emMap);
            request.setAttribute("st", startTime);
            request.setAttribute("et", endTime);
            request.setAttribute("tiaojian", tiaojian);
            request.setAttribute("totalNum", page.getTotal());
        } else {
            request.setAttribute("tiaojian", tiaojian);
            request.setAttribute("packageinformationList", "");
            request.setAttribute("st", startTime);
            request.setAttribute("et", endTime);
            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
            request.setAttribute("total", 1);
            request.setAttribute("totalNum", 0);
        }
        return new ModelAndView("mj/miejunshenhejieguo/miejunshenhe_jieguo");
    }

    /**
     * @param id
     * @param request
     * @param response
     */
    //灭菌--不合格记录
    @RequestMapping("/findByMiejunLiushui.do")
    @ResponseBody
    public Map<Object, Object> findByMiejunLiushui(String id, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<Object, Object> rtnMap = new HashMap<Object, Object>();
        try {
            map.put("miejunLiushui", id);
            List<ZsMiejunMingxi> lstMiejunmingxi = zsMiejunMingxiService.findWaitShenhe(map);
            rtnMap.put("lstMiejunmingxi", lstMiejunmingxi);
            rtnMap.put("state", "true");
            rtnMap.put("msg", "成功");
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/bd_ceshi.do")
    public ModelAndView bdCeshi(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        map.put("equipType", 2);
        List<ZsEquip> lstEquips = zsEquipService.findEquipListByType(map);
        request.setAttribute("lstEquips", lstEquips);
        return new ModelAndView("mj/bdceshi/bd_ceshi");
    }

    /**
     * @param
     * @param request
     * @param response
     */
    @RequestMapping("/startCeshi.do")
    @ResponseBody
    public Map<Object, Object> startCeshi(String testMan, ZsEquipCeshi zec, String ceshiEquipId, HttpServletRequest request,
                           HttpServletResponse response) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<Object, Object>();
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            Map<String, Object> ormap = new HashMap<>();
            ormap.put("ceshiEquipId", ceshiEquipId);
            ormap.put("ceshiDate", date);
            ormap.put("ceshiHege", 2);
            List<ZsEquipCeshi> youornot = zsEquipCeshiService.findByDateHegeState(ormap);
            // ZsEquipCeshi ceshiequip = new ZsEquipCeshi();

            TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(testMan);
            if (tbEmployee != null && tbEmployee.getEmployeeNo().equals(testMan)) {
                Map<String, Object> map = new HashMap<>();
                Map<String, Object> map1 = new HashMap<>();

                map.put("ceshiEquipId", ceshiEquipId);
                map.put("ceshiDate", date);
                map.put("ceshiHege", 1);
                map.put("ceshiState", 1);
                map1.put("ceshiEquipId", ceshiEquipId);
                map1.put("ceshiDate", date);
                map1.put("ceshiState", 0);
                Integer j = 0;
                List<ZsEquipCeshi> list1 = zsEquipCeshiService.findByDateHegeState(map1);
                List<ZsEquipCeshi> list = zsEquipCeshiService.findByDateHegeState(map);
                if (list.isEmpty() && list1.isEmpty()) {
                    if (youornot.size() == 1) {
                        System.out.println("今天该设备测试未合格");
                        // 修改该测试流水的信息
                        Map<String, Object> updateMap = new HashMap<String, Object>();
                        updateMap.put("ceshiLiushui", sdf.format(date) + seqService.seqVal("ceshino"));
                        updateMap.put("ceshiEquipId", ceshiEquipId);
                        updateMap.put("ceshiSoft", zec.getCeshiSoft());
                        updateMap.put("ceshirenTiaoma", testMan);
                        updateMap.put("ceshiKaishiTime", date);
                        updateMap.put("ceshiJieshuTime", null);
                        updateMap.put("ceshiDate", date);
                        updateMap.put("ceshiHege", 2);
                        updateMap.put("ceshiHege1", 0);
                        updateMap.put("ceshiState", 0);

                        zsEquipCeshiService.updateCeshi(updateMap);
                        j = 1;
                    }
                    if (j != 1) {
                        // ZsEquipCeshi equipCeshi = new ZsEquipCeshi();
                        zec.setCeshiDate(date);
                        // equipCeshi.setCeshiEquipId(zec.getCeshiEquipId());
                        zec.setCeshiHege(0);
                        zec.setCeshiState(0);
                        zec.setCeshiKaishiTime(date);
                        zec.setCeshirenTiaoma(zec.getCeshirenTiaoma());
                        // equipCeshi.setCeshiSoft(zec.getCeshiSoft());
                        zec.setCeshiLiushui(sdf.format(date) + seqService.seqVal("ceshino"));
                        zsEquipCeshiService.insert(zec);
                    }

                    rtnMap.put("state", "true");
                    rtnMap.put("msg", "保存成功");
                } else {
                    rtnMap.put("state", "false");
                    rtnMap.put("msg", "该设备今天已进行过测试或正在测试中");
                }
            } else {
                rtnMap.put("state", "false");
                rtnMap.put("msg", "无效审核人条码");
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "测试失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/bdCeshiShenhe.do")
    public ModelAndView ceshiShenhe(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        map.put("ceshiState", 0);
        map.put("ceshiDate", new Date());
        // map.put("ceshiHege", 0);
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        PageInfo<ZsEquipCeshi> page = zsEquipCeshiService.findByDateAndHegeAndState(map);
        request.setAttribute("lstCeshi", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        return new ModelAndView("mj/bdceshijieshu/bd_ceshi_jieshu");
    }

    /**
     * @param request
     * @param response
     */
    @RequestMapping("/endCeshi.do")
    @ResponseBody
    public Map<Object, Object> endCeshi(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        Map<Object, Object> rtnMap = new HashMap<>();
        Date date = new Date();
        String[] ceshiLiushui = request.getParameter("id").split(",");
        try {
            for (int i = 0; i < ceshiLiushui.length; i++) {
                map.put("ceshiJieshuTime", date);
                map.put("ceshiState", 1);
                map.put("ceshiLiushui", ceshiLiushui[i]);
                map.put("ceshiHege", 0);
                zsEquipCeshiService.updateEndtimeAndState(map);
                rtnMap.put("state", "true");
                rtnMap.put("msg", "成功");
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findShenhe.do")
    public ModelAndView findCeshiShenhe(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        map.put("ceshiState", 1);
        map.put("ceshiDate", new Date());
        map.put("ceshiHege", 0);
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        PageInfo<ZsEquipCeshi> page = zsEquipCeshiService.findByDateAndHegeAndState(map);
        request.setAttribute("lstCeshi", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        return new ModelAndView("mj/bdceshishenhe/bd_ceshi_shenhe");
    }

    /**
     * @param ceshiHege
     * @param ceshiLiushui
     * @param request
     * @param response
     */
    @RequestMapping("/clickShenhe.do")
    @ResponseBody
    public Map<Object, Object> clickShenhe(Integer[] ceshiHege, String[] ceshiLiushui, HttpServletRequest request,
            HttpServletResponse response) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        try {
            Integer hege;
            for (int i = 0; i < ceshiLiushui.length; i++) {
                hege = Integer.valueOf(ceshiHege[i]);
                if (hege == 1) {// 合格
                    map.put("ceshiHege", hege);
                    map.put("ceshiLiushui", ceshiLiushui[i]);
                    zsEquipCeshiService.updateEndtimeAndState(map);
                    rtnMap.put("state", "true");
                    rtnMap.put("msg", "成功");
                } else {// 2不合格
                    map.put("ceshiHege", hege);
                    map.put("ceshiLiushui", ceshiLiushui[i]);
                    zsEquipCeshiService.updateEndtimeAndState(map);
                    rtnMap.put("state", "true");
                    rtnMap.put("msg", "不通过");
                }
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    @RequestMapping("/miejunPackage")
    @ResponseBody
    public Map<String,Object> miejn(Integer type){
        Map<String,Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
//        map.put("peibaoZhuangtai", 1);
        map.put("peibaoHege", 1);
        map.put("mjLiushui", "mjLiushui");
        map.put("changjia", "changjia");
        map.put("peibaoren", "peibaoren");
        List<ZsPackageImformation> packageImformations = zsPackageInformationService.findAll(map);
        Iterator<ZsPackageImformation> iterator = packageImformations.iterator();

        while (iterator.hasNext()){
            ZsPackageImformation zsPackageImformation = iterator.next();
            TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();

            if(tbPackageMuban !=null){
                int print = tbPackageMuban.getPackagePrint();
                if(print==0){
                    iterator.remove();
                }
                if(type!=null&&type!=0){
                    Integer miejunFangshi = tbPackageMuban.getPackageMiejunFangshi();
                    if(type==1){
                        if(miejunFangshi==182){
                            iterator.remove();
                            continue;
                        }
                    }else if(type==2){
                        if(miejunFangshi==181){
                            iterator.remove();
                            continue;
                        }
                    }
                    if(type == 3){
                        if(zsPackageImformation.getPackageKeshi()!=null){
                            if(zsPackageImformation.getPackageKeshi()==15){
                                iterator.remove();
                                continue;
                            }

                        }
                    }
                    if(type == 4){
                        if(zsPackageImformation.getPackageKeshi()!=null){
                            if(zsPackageImformation.getPackageKeshi()!=15){
                                iterator.remove();
                                continue;
                            }

                        }
                    }
                }
            }else{
                iterator.remove();
            }
        }
        rtnMap.put("packageImformations",packageImformations);
        rtnMap.put("status",true);
        rtnMap.put("dictmap",StartUpOnLoadData.singleDataMap);
        return rtnMap;

    }


    /**
     * 查找所有配包合格（等待灭菌的包） 并跳转到灭菌准备页面
     * @param request
     * @param response
     * @return
     */
    //灭菌--灭菌准备
    @RequestMapping("/miejun.do")
    public ModelAndView miejun(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {

//        if (!packageImformations.isEmpty()) {
//            request.setAttribute("lstPeibao", packageImformations);
//            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
//            // request.setAttribute("total", page.getPages());
//        } else {
//            request.setAttribute("lstPeibao", "");
//            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
//            request.setAttribute("total", 1);
//        }
        // Map<String, Object> map1 = new HashMap<String,Object>();
        // List<ZsEquip> list = new ArrayList<ZsEquip>();
        /*
         * map1.put("ceshiDate", new Date()); map1.put("ceshiHege", 1);
         * List<ZsEquipCeshi> lstCeshi =
         * zsEquipCeshiService.findByDateHegeState(map1); for (ZsEquipCeshi
         * zsEquipCeshi : lstCeshi) {
         * list.add(zsEquipService.selectByPrimaryKey(
         * zsEquipCeshi.getCeshiEquipId())); }
         */

        // request.setAttribute("lstEquips", list);
        return new ModelAndView("/mj/miejun/miejun_kaishi");
        /* return new ModelAndView("/mj/equip_list"); */
    }

    @RequestMapping("/miejunAdd")
    public ModelAndView miejunAdd(){
        return new ModelAndView("/mj/miejun/miejun_addpackage");
    }

    @RequestMapping("/miejunAddPackage")
    @ResponseBody
    public Map<String,Object> miejunAddPackage(Integer type){
        Map<String,Object> rtnMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
//        map.put("peibaoZhuangtai", 1);
        map.put("peibaoHege", 1);
        map.put("mjLiushui", "mjLiushui");
        map.put("changjia", "changjia");
        map.put("peibaoren", "peibaoren");
        List<ZsPackageImformation> packageImformations = zsPackageInformationService.findMiejunLinChuang(map);
        Iterator<ZsPackageImformation> iterator = packageImformations.iterator();
        List<ZsPackageImformation> packageImformations1 = zsPackageInformationService.findAll(map);
        Iterator<ZsPackageImformation> iterator2 = packageImformations1.iterator();
        while (iterator.hasNext()){
            ZsPackageImformation zsPackageImformation = iterator.next();
            TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
            if(tbPackageMuban !=null){
                int print = tbPackageMuban.getPackagePrint();
                if(type!=null&&type!=0){
                    Integer miejunFangshi = tbPackageMuban.getPackageMiejunFangshi();
                    if(type==1){
                        if(miejunFangshi==182){
                            iterator.remove();
                            continue;
                        }
                    }else if(type==2){
                        if(miejunFangshi==181){
                            iterator.remove();
                            continue;
                        }
                    }
                }
                if(print==1){
                    iterator.remove();
                }
            }else{
                iterator.remove();
            }
        }

        while (iterator2.hasNext()){
            ZsPackageImformation zsPackageImformation = iterator2.next();
            TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
            if(tbPackageMuban !=null){
                int print = tbPackageMuban.getPackagePrint();
                if(type!=null&&type!=0){
                    Integer miejunFangshi = tbPackageMuban.getPackageMiejunFangshi();
                    if(type==1){
                        if(miejunFangshi==182){
                            iterator2.remove();
                            continue;
                        }
                    }else if(type==2){
                        if(miejunFangshi==181){
                            iterator2.remove();
                            continue;
                        }
                    }
                }
                if(print==1){
                    iterator2.remove();
                }
            }else{
                iterator2.remove();
            }
        }
        Map<Integer,List<String>> tiaomaMap= new HashMap<>();
        for (int i = 0; i < packageImformations1.size(); i++) {
            ZsPackageImformation zsPackageImformation = packageImformations1.get(i);
            Integer packageId = zsPackageImformation.getPackageId();
            if(tiaomaMap.containsKey(packageId)){
                List<String> list = tiaomaMap.get(packageId);
                list.add(zsPackageImformation.getPackageTiaoma());
                tiaomaMap.put(packageId,list);
            }else{
                List<String> list = new ArrayList<>();
                list.add(zsPackageImformation.getPackageTiaoma());
                tiaomaMap.put(packageId,list);
            }
        }
        for (int i = 0; i < packageImformations.size(); i++) {
            ZsPackageImformation zsPackageImformation = packageImformations.get(i);
            Integer packId = zsPackageImformation.getPackageId();
            List<String> tiaomaList = tiaomaMap.get(packId);
            String [] tiaomaArr = tiaomaList.toArray(new String [tiaomaList.size()]);
            zsPackageImformation.setPackageTiaomas(tiaomaArr);
        }
        rtnMap.put("packageImformations",packageImformations);
        rtnMap.put("status",true);
        rtnMap.put("dictmap",StartUpOnLoadData.singleDataMap);
        return rtnMap;
    }

    /**
     * 保存灭菌和灭菌明细 修改网篮 状态为2
     * 
     * 判断包的灭菌方式是否有误（查询包的灭菌方式和设备的灭菌方式）
     * 
     * @param
     * @param request
     * @param response
     */
    //灭菌--灭菌准备    点击保存
    @RequestMapping("/miejunSave.do")
    @ResponseBody
    public Map<Object, Object> miejunSave(String miejunShebeiTiaoma, String miejunMeijunrenTiaoma, String miejunRuanjianTiaoma,
            HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
        try {
            // 得到设备的灭菌category
            ZsSoft soft = zsSoftService.selectByPrimaryKey(Integer.valueOf(miejunRuanjianTiaoma));
            Map<String, Object> equipMap = new HashMap<>();
            equipMap.put("equipId", soft.getEquipId());
            Integer equipCategory = zsEquipService.findequipFangshi(equipMap).getEquipCategory();
            TbDict tbDict = tbDictService.selectByPrimaryKey(equipCategory);
            String equipMiejunFangshi = "";
            if (tbDict != null) {
                equipMiejunFangshi = tbDict.getDictName();
            }
            List<ZsPeibao> peibaoList = new ArrayList<>();
            List<ZsPackageImformation> packageImformations = new ArrayList<>();
            List<ZsMiejunMingxi> list1 = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            Date date = new Date();
            TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(miejunMeijunrenTiaoma);
            String miejunLiushui = sdf.format(date) + seqService.seqVal("miejunno");
            ZsMiejun zsMiejun = new ZsMiejun();
            zsMiejun.setMiejunLiushui(miejunLiushui);
            zsMiejun.setMiejunShebeiTiaoma(soft.getEquipId().toString());
            zsMiejun.setMiejunRuanjianTiaoma(miejunRuanjianTiaoma);
            zsMiejun.setMiejunMeijunrenTiaoma(miejunMeijunrenTiaoma.toUpperCase());
            zsMiejun.setMiejunKaishiTime(date);
            zsMiejun.setMiejunHege(0);// 未审核
            zsMiejun.setMiejunZhuangtai(0);// 准备灭菌
            zsMiejun.setRecordTime(date);
            List<String> miejunfangshis = new ArrayList<>();
            String[] baozhuangtiaomas = request.getParameter("baozhuangTiaoma").split(",");
            Long[] seqVal = seqService.seqValBatch("miejunmingxino", baozhuangtiaomas.length);
            // 得到包的灭菌方式
            for (int i = 0; i < baozhuangtiaomas.length; i++) {
                ZsPackageImformation packageImformation = zsPackageInformationService
                        .selectByPrimaryKey(baozhuangtiaomas[i]);
                Integer packageId = null;
                if (packageImformation != null) {
                    packageId = packageImformation.getPackageId();
                }
                TbPackageMuban packageMuban = packageImformation.getTbPackageMuban();
                if(packageMuban!=null){
                    Long packageMiejun = null;
                    if (packageMuban != null) {
                        packageMiejun = (long) packageMuban.getPackageMiejunFangshi();
                    }
                    Map<Long, TbDict> dictMap = StartUpOnLoadData.singleDataMap;
                    String packageMiejunFangshi = "";
                    if (dictMap.get(packageMiejun) != null) {
                        packageMiejunFangshi = dictMap.get(packageMiejun).getDictName();
                    }

                    if (!equipMiejunFangshi.equals(packageMiejunFangshi)) {
                        if (!miejunfangshis.contains(packageMiejunFangshi)) {
                            miejunfangshis.add(packageMiejunFangshi);
                        }
                    }
                }

                Map<String, Object> baoMap = new HashMap<>();
                baoMap.put("miejunWupinbaoTiaoma", baozhuangtiaomas[i]);
                ZsMiejunMingxi zsMiejunMingxi = new ZsMiejunMingxi();
                String miejunmingxiliushui = sdf.format(new Date()) + seqVal[i];
                zsMiejunMingxi.setMiejunLiushui(miejunLiushui);
                zsMiejunMingxi.setMiejunMingxiLiushui(miejunmingxiliushui);
                zsMiejunMingxi.setMiejunZhunbeiLiushui(packageImformation.getPackageName());
                zsMiejunMingxi.setMiejunHege(0);
                zsMiejunMingxi.setRecordTime(date);
                zsMiejunMingxi.setMiejunWupinbaoTiaoma(baozhuangtiaomas[i]);
                list1.add(zsMiejunMingxi);
                ZsPeibao peibao = new ZsPeibao();
                peibao.setPeibaoZhuangtai(2);
                peibao.setPeibaoBaozhuangTiaoma(baozhuangtiaomas[i]);
                peibaoList.add(peibao);
                ZsPackageImformation zsPackageImformation = new ZsPackageImformation();
                zsPackageImformation.setPackageLiuchengZhuangtai(5);
                zsPackageImformation.setMiejunLiushui(miejunLiushui);
                zsPackageImformation.setMiejunShebeiTiaoma(soft.getEquipId().toString());
                zsPackageImformation.setMiejunRuanjianTiaoma(miejunRuanjianTiaoma);
                zsPackageImformation.setMiejunHege(0);
                zsPackageImformation.setMiejunKaishiTime(date);
                zsPackageImformation.setMiejunZhuangtai(0);
                zsPackageImformation.setPackageTiaoma(baozhuangtiaomas[i]);
                if (tbEmployee != null) {
                    zsPackageImformation.setMiejunMeijunrenTiaoma(miejunMeijunrenTiaoma.toUpperCase());
                    packageImformations.add(zsPackageImformation);
                }

            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String data [] = new String[5];

            data[0] = StartUpOnLoadData.equipSinMap.get(Long.valueOf(zsMiejun.getMiejunShebeiTiaoma()));
            data[1] = StartUpOnLoadData.softSinMap.get(Long.valueOf(zsMiejun.getMiejunRuanjianTiaoma()));
            data[2] = simpleDateFormat.format(zsMiejun.getMiejunKaishiTime());
            data[3] = StartUpOnLoadData.emMap.get(zsMiejun.getMiejunMeijunrenTiaoma());
            data[4] = miejunLiushui;
            if (packageImformations.size() != 0) {
                if (miejunfangshis.size() >= 1) {
                    rtnMap.put("state", "false");
                    rtnMap.put("msg", "有不匹配的包");
                } else {
                    zsPackageInformationService.updateByBatch(packageImformations);
                    miejunService.insert(zsMiejun);
                    zsMiejunMingxiService.insertBatch(list1);
                    zsPeibaoService.updateBatchByBao(peibaoList);
                    rtnMap.put("state", "true");
                    rtnMap.put("datas",data);
                    rtnMap.put("msg", "开始成功");
                }
            } else {
                rtnMap.put("state", "false");
                rtnMap.put("msg", "无效灭菌人条码");
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "开始失败");
            e.printStackTrace();
        }
        return rtnMap;
    }


    @RequestMapping("/miejunSaveAndroid")
    @ResponseBody
    public Map<Object, Object> miejunSaveAndroid(String pdata ,String miejunMeijunrenTiaoma, String miejunRuanjianTiaoma) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<>();
//        = request.getParameter("baozhuangTiaoma").split(",")
        System.out.print(pdata);
        JSONObject json = JSON.parseObject(pdata);
        miejunRuanjianTiaoma = json.getString("itcode");
        miejunMeijunrenTiaoma = json.getString("peoplecode");
        JSONArray packageTiaomaArr = json.getJSONArray("datas");
        String [] baozhuangtiaomas  = new String[packageTiaomaArr.size()];
        for (int i = 0; i < packageTiaomaArr.size(); i++) {
            baozhuangtiaomas[i] = JSON.parseObject(packageTiaomaArr.get(i).toString()).getString("packageTiaoma");
        }
        Map<String,Object> map = new HashMap<>();
        try {
            ZsSoft soft = zsSoftService.selectByPrimaryKey(Integer.valueOf(miejunRuanjianTiaoma));
            if (soft != null) {
                ZsEquip equip = equipService.selectByPrimaryKey(soft.getEquipId());
                if (equip != null) {
                    map.put("ceshiEquipId", soft.getEquipId());
                    map.put("ceshiDate", new Date());
                    map.put("ceshiHege", 1);
                    List<ZsEquipCeshi> equipCeshis  = equipCeshiSercie.findByDateHegeState(map);
                    if (!equipCeshis.isEmpty()) {
                        rtnMap.put("msg", "扫码成功");
                        rtnMap.put("state", "true");
                        rtnMap.put("soft", soft);
                        rtnMap.put("equip", equip);

                        // 得到设备的灭菌category
                        Map<String, Object> equipMap = new HashMap<>();
                        equipMap.put("equipId", soft.getEquipId());
                        Integer equipCategory = zsEquipService.findequipFangshi(equipMap).getEquipCategory();
                        TbDict tbDict = tbDictService.selectByPrimaryKey(equipCategory);
                        String equipMiejunFangshi = "";
                        if (tbDict != null) {
                            equipMiejunFangshi = tbDict.getDictName();
                        }
                        List<ZsPeibao> peibaoList = new ArrayList<>();
                        List<ZsPackageImformation> packageImformations = new ArrayList<>();
                        List<ZsMiejunMingxi> list1 = new ArrayList<>();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
                        Date date = new Date();
                        TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(miejunMeijunrenTiaoma);
                        String miejunLiushui = sdf.format(date) + seqService.seqVal("miejunno");
                        ZsMiejun zsMiejun = new ZsMiejun();
                        zsMiejun.setMiejunLiushui(miejunLiushui);
                        zsMiejun.setMiejunShebeiTiaoma(soft.getEquipId().toString());
                        zsMiejun.setMiejunRuanjianTiaoma(miejunRuanjianTiaoma);
                        zsMiejun.setMiejunMeijunrenTiaoma(miejunMeijunrenTiaoma.toUpperCase());
                        zsMiejun.setMiejunKaishiTime(date);
                        zsMiejun.setMiejunHege(0);// 未审核
                        zsMiejun.setMiejunZhuangtai(0);// 准备灭菌
                        zsMiejun.setRecordTime(date);
                        List<String> miejunfangshis = new ArrayList<>();
                        Long[] seqVal = seqService.seqValBatch("miejunmingxino", baozhuangtiaomas.length);
                        // 得到包的灭菌方式
                        for (int i = 0; i < baozhuangtiaomas.length; i++) {
                            ZsPackageImformation packageImformation = zsPackageInformationService
                                    .selectByPrimaryKey(baozhuangtiaomas[i]);
                            Integer packageId = null;
                            if (packageImformation != null) {
                                packageId = packageImformation.getPackageId();
                            }
                            TbPackageMuban packageMuban = tbPackageMubanService.selectByPrimaryKey(String.valueOf(packageId));
                            Long packageMiejun = null;
                            if (packageMuban != null) {
                                packageMiejun = (long) packageMuban.getPackageMiejunFangshi();
                            }
                            Map<Long, TbDict> dictMap = StartUpOnLoadData.singleDataMap;
                            String packageMiejunFangshi = "";
                            if (dictMap.get(packageMiejun) != null) {
                                packageMiejunFangshi = dictMap.get(packageMiejun).getDictName();
                            }

                            if (!equipMiejunFangshi.equals(packageMiejunFangshi)) {
                                if (!miejunfangshis.contains(packageMiejunFangshi)) {
                                    miejunfangshis.add(packageMiejunFangshi);
                                }
                            }
                        }

                        ZsMiejunMingxi zsMiejunMingxi = null;
                        for (int i = 0; i < baozhuangtiaomas.length; i++) {
                            Map<String, Object> baoMap = new HashMap<>();
                            baoMap.put("miejunWupinbaoTiaoma", baozhuangtiaomas[i]);
                            zsMiejunMingxi = new ZsMiejunMingxi();
                            String miejunmingxiliushui = sdf.format(new Date()) + seqVal[i];
                            zsMiejunMingxi.setMiejunLiushui(miejunLiushui);
                            zsMiejunMingxi.setMiejunMingxiLiushui(miejunmingxiliushui);
                            zsMiejunMingxi.setMiejunHege(0);
                            zsMiejunMingxi.setRecordTime(date);
                            zsMiejunMingxi.setMiejunWupinbaoTiaoma(baozhuangtiaomas[i]);
                            list1.add(zsMiejunMingxi);
                            ZsPeibao peibao = new ZsPeibao();
                            peibao.setPeibaoZhuangtai(2);
                            peibao.setPeibaoBaozhuangTiaoma(baozhuangtiaomas[i]);
                            peibaoList.add(peibao);
                            ZsPackageImformation zsPackageImformation = new ZsPackageImformation();
                            zsPackageImformation.setPackageLiuchengZhuangtai(5);
                            zsPackageImformation.setMiejunLiushui(miejunLiushui);
                            zsPackageImformation.setMiejunShebeiTiaoma(soft.getEquipId().toString());
                            zsPackageImformation.setMiejunRuanjianTiaoma(miejunRuanjianTiaoma);
                            zsPackageImformation.setMiejunHege(0);
                            zsPackageImformation.setMiejunKaishiTime(date);
                            zsPackageImformation.setMiejunZhuangtai(0);
                            zsPackageImformation.setPackageTiaoma(baozhuangtiaomas[i]);
                            if (tbEmployee != null) {
                                zsPackageImformation.setMiejunMeijunrenTiaoma(miejunMeijunrenTiaoma.toUpperCase());
                                packageImformations.add(zsPackageImformation);
                            }
                        }
                        if (packageImformations.size() != 0) {
                            if (miejunfangshis.size() >= 1) {
                                rtnMap.put("state", "false");
                                rtnMap.put("msg", "有不匹配的包");
                            } else {
                                zsPackageInformationService.updateByBatch(packageImformations);
                                miejunService.insert(zsMiejun);
                                zsMiejunMingxiService.insertBatch(list1);
                                zsPeibaoService.updateBatchByBao(peibaoList);
                                rtnMap.put("state", "true");
                                rtnMap.put("msg", "开始成功");
                            }
                        } else {
                            rtnMap.put("state", "false");
                            rtnMap.put("msg", "无效灭菌人条码");
                        }
                    }else {
                        rtnMap.put("msg", "该设备未进行BD测试或不是灭菌设备");
                        rtnMap.put("state", "fasle");
                    }
                }else {
                    rtnMap.put("msg", "该设备无此程序");
                    rtnMap.put("state", "fasle");
                }
            }else {
                rtnMap.put("msg", "无此程序");
                rtnMap.put("state", "fasle");
            }

        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "开始失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    /**
     * 从灭菌明细表查
     * @param request
     * @param response
     * @return
     */
    // 灭菌--化学监测审核    查找所有灭菌完成，等待化学检测审核的包（灭菌明细表）
    @RequestMapping("/waitShenhe.do")
    public ModelAndView waitShenhe(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map2 = new HashMap<>();
        // ---------------灭菌第一次审核不通过的数据还在该页面显示可继续下一次审核
//        List<ZsMiejunMingxi> list1 = zsMiejunMingxiService.findWaitShenhe(map1);
//        for (ZsMiejunMingxi zsMiejunMingxi : list1) {
//            miejunLiushui = zsMiejunMingxi.getMiejunLiushui();
//            zsMiejun = miejunService.selectByPrimaryKey(miejunLiushui);
//            if (zsMiejun != null) {
//                if
// == 2) {
//                    mjlsList.add(zsMiejun.getMiejunLiushui());
//                }
//            }
//        }
        // ---------------
//        List<ZsMiejunMingxi> list = zsMiejunMingxiService.findWaitShenhe(map);
//        for (ZsMiejunMingxi zsMiejunMingxi : list) {
//            miejunLiushui = zsMiejunMingxi.getMiejunLiushui();
//            zsMiejun = miejunService.selectByPrimaryKey(miejunLiushui);
//            if (zsMiejun != null) {
//                if (zsMiejun.getMiejunZhuangtai() == 2) {
//                    mjlsList.add(zsMiejun.getMiejunLiushui());
//                }
//            }
////        }

        //查询出未审核 和不合格的
        map2.put("huaxueshenhe","huaxueshenhe");
        map2.put("miejunZhuangtai",2);
        map2.put("pageNo", pageNo);
        map2.put("pageSize", Const.PAGE_SIZE);
        map2.put("miejunshenhe", "miejunshenhe");
        PageInfo<ZsMiejun> page = miejunService.findAllMiejun(map2);
        if(page.getList().size()!=0){
            request.setAttribute("lstMiejun", page.getList());
            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
            request.setAttribute("total", page.getPages());
            return new ModelAndView("/mj/miejunshenhe/miejunshenhe");
        }else{
            request.setAttribute("lstMiejun", "");
            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
            request.setAttribute("total", 1);
            return new ModelAndView("/mj/miejunshenhe/miejunshenhe");
        }

//        if (mjlsList.size() != 0) {
//
//        } else {
//            request.setAttribute("lstMiejun", "");
//            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
//            request.setAttribute("total", 1);
//            return new ModelAndView("/mj/miejunshenhe/miejunshenhe");
//        }
    }

    @RequestMapping("/miejunList.do")
    public ModelAndView miejunList(String searchTxt, HttpServletRequest request, HttpServletResponse response)throws BusinessException{
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map2 = new HashMap<>();
        map2.put("pageNo", pageNo);
        map2.put("pageSize", Const.PAGE_SIZE);
        map2.put("searchTxt",searchTxt);
        // ---------------灭菌第一次审核不通过的数据还在该页面显示可继续下一次审核
        PageInfo<ZsMiejun> page = miejunService.findAllMiejun(map2);
        request.setAttribute("lstMiejun", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        return new ModelAndView("/mj/miejunshenhejieguo/miejun_jieguo");
    }


    @RequestMapping("/waitShenheAndroid")
    @ResponseBody
    public Map<String,Object> waitShenheAndroid() throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String,Object> rtnMap = new HashMap<>();
        List<String> mjlsList = new ArrayList<>();
        map.put("miejunHege", 0);
        // ---------------灭菌第一次审核不通过的数据还在该页面显示可继续下一次审核
        Map<String, Object> map1 = new HashMap<>();
        map1.put("miejunHege", 1);
        ZsMiejun zsMiejun = null;
        String miejunLiushui = null;
        List<ZsMiejunMingxi> list1 = zsMiejunMingxiService.findWaitShenhe(map1);
        for (ZsMiejunMingxi zsMiejunMingxi : list1) {
            miejunLiushui = zsMiejunMingxi.getMiejunLiushui();
            zsMiejun = miejunService.selectByPrimaryKey(miejunLiushui);
            if (zsMiejun != null) {
                if (zsMiejun.getMiejunZhuangtai() == 2) {
                    mjlsList.add(zsMiejun.getMiejunLiushui());
                }
            }
        }
        // ---------------
        List<ZsMiejunMingxi> list = zsMiejunMingxiService.findWaitShenhe(map);
        for (ZsMiejunMingxi zsMiejunMingxi : list) {
            miejunLiushui = zsMiejunMingxi.getMiejunLiushui();
            zsMiejun = miejunService.selectByPrimaryKey(miejunLiushui);
            if (zsMiejun != null) {
                if (zsMiejun.getMiejunZhuangtai() == 2) {
                    mjlsList.add(zsMiejun.getMiejunLiushui());
                }
            }
        }
        List<ZsMiejun> zsMiejuns = new ArrayList<>();
        if (mjlsList.size() != 0) {
            map2.put("miejunArr", mjlsList);
            map2.put("miejunshenhe", "miejunshenhe");
            zsMiejuns= miejunService.findAllMiejunNoPage(map2);
            rtnMap.put("datas",zsMiejuns);
        }else{
            rtnMap.put("datas",zsMiejuns);
            rtnMap.put("msg","没有待灭菌审核的数据");
        }
        return rtnMap;
    }

    @RequestMapping("/mjLiushuiList")
    @ResponseBody
    public Map<String,Object> mjLiushuiList(String data){
        Map<String,Object> rtnMap = new HashMap<>();
        Map<String,Object> map = new HashMap<>();
        map.put("miejunLiushui",data);
        List<ZsMiejunMingxi> zsMiejunMingxis = null;
        if(data!=null&&!"".equals(data)){
            zsMiejunMingxis  = zsMiejunMingxiService.findWaitShenhe(map);
        }
        rtnMap.put("status",true);
        rtnMap.put("datas",zsMiejunMingxis);
        return rtnMap;
    }

    /**
     * @param searchTxt
     *                  输入框搜索条件
     * @param request
     * @param response
     */
    // 灭菌--生物监测审核   查找所有等待生物检测审核的包（灭菌明细表）
    @RequestMapping("/miejunTwo.do")
    public ModelAndView miejunTwo(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map2 = new HashMap<>();

        map2.put("miejunHege", 2);
        map2.put("huaxueshenhe","huaxueshenhe");
        map2.put("miejunZhuangtai",2);
        map2.put("pageNo", pageNo);
        map2.put("pageSize", Const.PAGE_SIZE);
        map2.put("miejunshenhe", "miejunshenhe");
        PageInfo<ZsMiejun> page = miejunService.findAllMiejun(map2);
        if(page.getList().size()!=0){
            request.setAttribute("lstMiejun", page.getList());
            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
            request.setAttribute("total", page.getPages());
            return new ModelAndView("mj/miejunshenhe/miejun_shenhe1");
        }{
            request.setAttribute("lstMiejun", "");
            request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
            request.setAttribute("total", 1);
            return new ModelAndView("mj/miejunshenhe/miejun_shenhe1");

        }



    }

    //灭菌--BD测试查询   查找所有的BD测试清单（灭菌明细表）
    @RequestMapping("/findCeshi.do")
    public ModelAndView findCeshi(String searchTxt, String tiaojian, HttpServletRequest request,
            HttpServletResponse response) throws BusinessException, ParseException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<>();
        String st = request.getParameter("startTime");
        String et = request.getParameter("endTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        if (StringUtils.isNotBlank(st)) {
            startTime = sdf.parse(st);
            map.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(et)) {
            endTime = sdf.parse(et);
            map.put("endTime", endTime);
        }
        if (StringUtils.isBlank(st) && StringUtils.isBlank(et)) {
            map.put("startTime", lastDayWholePointDate(new Date()));
            map.put("endTime", new Date());
        }
        map.put("ceshiState", 1);// 测试完成
        map.put("ceshiDate", new Date());// 测试日期为今天
        map.put("ceshiHege1", 0);
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        PageInfo<ZsEquipCeshi> page = zsEquipCeshiService.findByDateAndHegeAndState(map);
        request.setAttribute("lstCeshi", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        request.setAttribute("st", startTime);
        request.setAttribute("et", endTime);
        return new ModelAndView("mj/bdceshichaxun/bdceshichaxun");
    }

    @RequestMapping("/findQixieList.do")
    public ModelAndView findQixieList(String id, String name, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Integer sid = Integer.valueOf(id);
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("packageId", sid);
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        PageInfo<TbProduct> page = tbProductService.findQixieByPackagePage(map);
        request.setAttribute("total", page.getPages());
        request.setAttribute("tbproductList", page.getList());
        request.setAttribute("packageId", sid);
        request.setAttribute("packname", name);
        return new ModelAndView("/mj/miejun/miejun_productlist");
    }

    @RequestMapping("/saomiaobao.do")
    @ResponseBody
    public Map saomiaobao(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<Object, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String list = request.getParameter("dataList");
            JSONArray jsonArray = JSON.parseArray(list);
            String[] baoArr = new String[jsonArray.size()];
            Integer[] baoId = new Integer[jsonArray.size()];
            // 消除jsonpackageTiaoma 重复的json数据。
            JSONObject jot = null;
            for (int m = 0; m < jsonArray.size(); m++) {
                jot = jsonArray.getJSONObject(m);
                baoArr[m] = jot.getString("bao");
            }
            map.put("packageTiaomaArr", baoArr);
            if (baoArr.length != 0) {
                List<ZsPackageImformation> zsPackList = zsPackageInformationService.findAll(map);
                for (int i = 0; i < zsPackList.size(); i++) {
                    ZsPackageImformation zsPack = zsPackList.get(i);
                    baoId[i] = zsPack.getPackageId();
                }
                map.put("packageIdArr", baoId);
                List<TbPackageMuban> tbPackList = tbPackageMubanService.findAllPackageNoPageList(map);
                rtnMap.put("tbPackList", tbPackList);
                rtnMap.put("dictMap", StartUpOnLoadData.singleDataMap);
                rtnMap.put("status", "true");
                rtnMap.put("msg", "编辑成功");
            }
        } catch (Exception e) {
            rtnMap.put("status", "false");
            rtnMap.put("msg", "编辑失败");
            e.printStackTrace();
        }
        return rtnMap;
    }

    //灭菌--灭菌     查找所有BD测试合格的灭菌设备 以及 各设备对应的准备好的灭菌信息
    @RequestMapping("/miejunequip.do")
    public ModelAndView miejunequip(HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        List<ZsEquip> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Map<String, String[]> equipIdmjlu = new HashMap<>();
        map1.put("ceshiDate", new Date());
        map1.put("ceshiHege", 1);
        map1.put("miejunZhuangtai", 0);
        String[] mjLiushuiSoftId = null;
        List<String> shebeiTiaomas = new ArrayList<>();
        List<ZsMiejun> miejuns = miejunService.findAllMiejunNoPage(map1);
        List<ZsEquipCeshi> lstCeshi = zsEquipCeshiService.findByDateHegeState(map1);
        ZsMiejun miejun = null;
        for (ZsEquipCeshi zsEquipCeshi : lstCeshi) {
            map.put("miejunShebeiTiaoma", zsEquipCeshi.getCeshiEquipId());
            map.put("miejunZhuangtai", 1);
            miejun = miejunService.findOne(map);
            if (miejun != null) {
                if (!shebeiTiaomas.contains(miejun.getMiejunShebeiTiaoma())) {
                    mjLiushuiSoftId = new String[3];
                    shebeiTiaomas.add(miejun.getMiejunShebeiTiaoma());
                    mjLiushuiSoftId[0] = miejun.getMiejunLiushui();
                    mjLiushuiSoftId[1] = miejun.getMiejunRuanjianTiaoma();
                    mjLiushuiSoftId[2] = sdf.format(miejun.getMiejunKaishiTime());
                    equipIdmjlu.put(miejun.getMiejunShebeiTiaoma(), mjLiushuiSoftId);
                }
            }
            list.add(zsEquipService.selectByPrimaryKey(zsEquipCeshi.getCeshiEquipId()));
        }
        Map<String,Object> map3 = new HashMap<>();
        map.put("equipType", 2);
        map.put("equipCategory",182);
        List<ZsEquip> equips = zsEquipService.findAllZsEquipListNoPage(map);
        for (int i = 0; i < equips.size(); i++) {
            map.put("miejunShebeiTiaoma", equips.get(i).getEquipId());
            map.put("miejunZhuangtai", 1);
            miejun = miejunService.findOne(map);
            if (miejun != null) {
                if (!shebeiTiaomas.contains(miejun.getMiejunShebeiTiaoma())) {
                    mjLiushuiSoftId = new String[3];
                    shebeiTiaomas.add(miejun.getMiejunShebeiTiaoma());
                    mjLiushuiSoftId[0] = miejun.getMiejunLiushui();
                    mjLiushuiSoftId[1] = miejun.getMiejunRuanjianTiaoma();
                    mjLiushuiSoftId[2] = sdf.format(miejun.getMiejunKaishiTime());
                    equipIdmjlu.put(miejun.getMiejunShebeiTiaoma(), mjLiushuiSoftId);
                }
            }

            list.add(equips.get(i));
        }
        if (!list.isEmpty()) {
            request.setAttribute("lstEquips", list);
        }
        /*
         * else { request.setAttribute("lstEquips", ""); }
         */
        if (!miejuns.isEmpty()) {
            request.setAttribute("lstMiejuns", miejuns);
        }
        request.setAttribute("miejunIng", JSON.toJSON(shebeiTiaomas));
        request.setAttribute("eqpIdMjls", JSON.toJSON(equipIdmjlu));
        request.setAttribute("softAll", JSON.toJSON(StartUpOnLoadData.softMap));
        request.setAttribute("empMap", StartUpOnLoadData.emMap);
        return new ModelAndView("/mj/shebeiguanli/equip_list");
    }

    @RequestMapping("/equipbao.do")
    public ModelAndView equipbao(String id, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        map.put("miejunShebeiTiaoma", id);
        map.put("liushuiNotnull", "liushuiNotnull");
        map.put("jieshuTime", "jieshuTime");
        List<ZsPackageImformation> packageImformations = zsPackageInformationService.findAll(map);
        if (!packageImformations.isEmpty()) {
            request.setAttribute("packageImformations", packageImformations);
        } else {
            request.setAttribute("packageImformations", "");
        }
        request.setAttribute("dictmap", StartUpOnLoadData.singleDataMap);
        request.setAttribute("emmap", StartUpOnLoadData.emMap);
        request.setAttribute("equipMap", StartUpOnLoadData.equipMap);
        return new ModelAndView("/mj/shebeiguanli/equip_bao");
    }

    //灭菌--不合格记录:(查询灭菌不合格的包的灭菌信息)
    @RequestMapping("/buhegerecord.do")
    public ModelAndView buhegerecord(String searchTxt, String tiaojian, String st, String et,
            HttpServletRequest request, HttpServletResponse response) throws BusinessException {
        Integer pageNo = request.getParameter("pageIndex") == null ? 1 : Integer.valueOf(request
                .getParameter("pageIndex"));
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date startTime = null;
        Date endTime = null;
        List<String> miejunLiushuList = new ArrayList<String>();
        try {
            if (StringUtils.isNotBlank(st)) {
                startTime = sdf.parse(st);
                map.put("startTime", startTime);
            }
            if (StringUtils.isNotBlank(et)) {
                endTime = sdf.parse(et);
                map.put("endTime", endTime);
            }
            if (StringUtils.isBlank(st) && StringUtils.isBlank(et)) {
                map.put("endTime", new Date());
                map.put("startTime", lastDayWholePointDate(new Date()));
            }
        } catch (ParseException e) {

            e.printStackTrace();
        }
        List<ZsMiejunBuhege> miejunBuheges = miejunbuhegeService.findAll(map);
        for (ZsMiejunBuhege miejunBuhege : miejunBuheges) {
            if (!miejunLiushuList.contains(miejunBuhege.getMiejunLiushui())) {
                miejunLiushuList.add(miejunBuhege.getMiejunLiushui());
            }
        }
        if (StringUtils.isNotBlank(tiaojian)) {
            map.put("tiaojian", tiaojian);
        }

        if (!miejunLiushuList.isEmpty()) {
            map.put("miejunArr", miejunLiushuList);
        } else {
            miejunLiushuList.add("0");
            map.put("miejunArr", miejunLiushuList);
        }
        map.put("pageNo", pageNo);
        map.put("pageSize", Const.PAGE_SIZE);
        map.put("miejunbuhege", "miejunbuhege");
        PageInfo<ZsMiejun> page = miejunService.findAllMiejun(map);
        request.setAttribute("miejunList", page.getList());
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        request.setAttribute("total", page.getPages());
        request.setAttribute("equipMap", StartUpOnLoadData.equipMap);
        request.setAttribute("softMap", StartUpOnLoadData.softMap);
        request.setAttribute("emMap", StartUpOnLoadData.emMap);
        request.setAttribute("st", startTime);
        request.setAttribute("et", endTime);
        request.setAttribute("tiaojian", tiaojian);
        return new ModelAndView("/mj/miejunbuhege/buhege_record");
    }

    @RequestMapping("bdCeshi.do")
    public ModelAndView BDCeshi(String searchTxt, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        // ----------------bd测试
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("equipType", 2);
        List<ZsEquip> lstEquips = zsEquipService.findEquipListByType(map);
        request.setAttribute("lstEquips", lstEquips);
        // -------------结束测试
        // Integer pageNo = request.getParameter("pageIndex")==null? 1
        // :Integer.valueOf(request.getParameter("pageIndex"));
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("ceshiState", 0);
        map2.put("ceshiDate", new Date());
        List<ZsEquipCeshi> zs = zsEquipCeshiService.findByDateHegeState(map2);
        request.setAttribute("lstCeshi", zs);

        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("ceshiState", 1);
        map3.put("ceshiDate", new Date());
        map3.put("ceshiHege", 0);
        List<ZsEquipCeshi> shenhes = zsEquipCeshiService.findByDateHegeState(map3);
        request.setAttribute("lstShenhe", shenhes);
        request.setAttribute("searchTxt", searchTxt == null ? "" : searchTxt);
        return new ModelAndView("mj/bdceshi/bdceshi");
    }

    //灭菌--灭菌    点击保存
    @RequestMapping("equippackaSave.do")
    @ResponseBody
    public Map equippackaSave(String liushui, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<Object, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        List<ZsPackageImformation> infoImformations = new ArrayList<ZsPackageImformation>();
        ZsMiejun miejun = null;
        if (StringUtils.isNotBlank(liushui)) {
            miejun = miejunService.selectByPrimaryKey(liushui);
            map.put("miejunLiushui", miejun.getMiejunLiushui());
            map.put("miejunZhuangtai", 0);
            List<ZsPackageImformation> packimImformations = zsPackageInformationService.findAll(map);
            for (ZsPackageImformation zsPackageImformation : packimImformations) {
                zsPackageImformation.setMiejunZhuangtai(1);
                zsPackageImformation.setMiejunKaishiTime(new Date());
                infoImformations.add(zsPackageImformation);
            }
            miejun.setMiejunZhuangtai(1);
            miejun.setMiejunKaishiTime(new Date());
            miejunService.updateByPrimaryKey(miejun);
            if(infoImformations.size()!=0){
                zsPackageInformationService.updateByBatch(infoImformations);
            }
            rtnMap.put("msg", "保存成功");
            rtnMap.put("status", "true");
        } else {
            rtnMap.put("msg", "保存失败");
            rtnMap.put("status", "false");
        }
        return rtnMap;
    }

    //灭菌--灭菌   点击结束
    @RequestMapping("equippackEnd.do")
    @ResponseBody
    @Transactional
    public Map equippackEnd(String equipid, HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        Map<Object, Object> rtnMap = new HashMap<Object, Object>();
        Map<String, Object> map = new HashMap<String, Object>();
        List<ZsPackageImformation> infoImformations = new ArrayList<ZsPackageImformation>();
        ZsMiejun miejun = null;
        if (StringUtils.isNotBlank(equipid)) {
            map.put("miejunShebeiTiaoma", equipid);
            map.put("miejunZhuangtai", 1);
            miejun = miejunService.findOne(map);
            List<ZsPackageImformation> packageImformations = zsPackageInformationService.findAll(map);
            for (ZsPackageImformation zsPackageImformation : packageImformations) {
                zsPackageImformation.setMiejunJieshuTime(new Date());
                zsPackageImformation.setMiejunZhuangtai(2);
                infoImformations.add(zsPackageImformation);
            }
            miejun.setMiejunZhuangtai(2);
            miejun.setMiejunJieshuTime(new Date());
            miejunService.updateByPrimaryKey(miejun);
            if(infoImformations!=null &&infoImformations.size()!=0){
                zsPackageInformationService.updateByBatch(infoImformations);
            }
            rtnMap.put("msg", "保存成功");
            rtnMap.put("status", "true");
        } else {
            rtnMap.put("msg", "保存失败");
            rtnMap.put("status", "false");
        }
        return rtnMap;
    }

    public static Date lastDayWholePointDate(Date date) {
        GregorianCalendar gc = new GregorianCalendar();
        System.out.println(gc);
        gc.setTime(date);
        if ((gc.get(gc.HOUR_OF_DAY) == 0) && (gc.get(gc.MINUTE) == 0) && (gc.get(gc.SECOND) == 0)) {
            return new Date(date.getTime());
        } else {
            Date date2 = new Date(date.getTime() - gc.get(gc.HOUR_OF_DAY) * 60 * 60 * 1000 - gc.get(gc.MINUTE) * 60
                    * 1000 - gc.get(gc.SECOND) * 1000);
            return date2;
        }
    }

    @RequestMapping("miejunexamine.do")
    public ModelAndView miejunexamine(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView("mj/miejun_examine/examine");
    }


    @RequestMapping("/sterilizeAndroid")
    @ResponseBody
    public Map<Object, Object> sterilizeAndroid(String pdata
                                          ) throws BusinessException {

        pdata = URLDecoder.decode(URLDecoder.decode(pdata));
        System.out.print(pdata);
        JSONObject json = JSON.parseObject(pdata);
        String miejunRuanjianTiaoma = json.getString("softCode");
        String miejunMeijunrenTiaoma = json.getString("manCode");
        JSONArray packageBarcodeArr = json.getJSONArray("packageBarcodeArr");
        String[] baozhuangtiaomas = packageBarcodeArr.toArray(new String [packageBarcodeArr.size()]);

        Map<Object, Object> rtnMap = new HashMap<>();
        try {
            // 得到设备的灭菌category
            ZsSoft soft = zsSoftService.selectByPrimaryKey(Integer.valueOf(miejunRuanjianTiaoma));
            Map<String, Object> equipMap = new HashMap<>();
            equipMap.put("equipId", soft.getEquipId());
            Integer equipCategory = zsEquipService.findequipFangshi(equipMap).getEquipCategory();
            TbDict tbDict = tbDictService.selectByPrimaryKey(equipCategory);
            String equipMiejunFangshi = "";
            if (tbDict != null) {
                equipMiejunFangshi = tbDict.getDictName();
            }
            List<ZsPeibao> peibaoList = new ArrayList<>();
            List<ZsPackageImformation> packageImformations = new ArrayList<>();
            List<ZsMiejunMingxi> list1 = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            Date date = new Date();
            TbEmployee tbEmployee = tbEmployeeService.selectByEmployeeNo(miejunMeijunrenTiaoma);
            String miejunLiushui = sdf.format(date) + seqService.seqVal("miejunno");
            ZsMiejun zsMiejun = new ZsMiejun();
            zsMiejun.setMiejunLiushui(miejunLiushui);
            zsMiejun.setMiejunShebeiTiaoma(soft.getEquipId().toString());
            zsMiejun.setMiejunRuanjianTiaoma(miejunRuanjianTiaoma);
            zsMiejun.setMiejunMeijunrenTiaoma(miejunMeijunrenTiaoma);
            zsMiejun.setMiejunKaishiTime(date);
            zsMiejun.setMiejunHege(0);// 未审核
            zsMiejun.setMiejunZhuangtai(0);// 准备灭菌
            zsMiejun.setRecordTime(date);
            List<String> miejunfangshis = new ArrayList<>();
            Long[] seqVal = seqService.seqValBatch("miejunmingxino", baozhuangtiaomas.length);
            // 得到包的灭菌方式
            for (int i = 0; i < baozhuangtiaomas.length; i++) {
                ZsPackageImformation packageImformation = zsPackageInformationService
                        .selectByPrimaryKey(baozhuangtiaomas[i]);
                if(packageImformation.getMiejunLiushui()!=null&&packageImformation.getPeibaoGuanbaoTiaoma()!=null){
                    Integer packageId = null;
                    if (packageImformation != null) {
                        packageId = packageImformation.getPackageId();
                    }
                    TbPackageMuban packageMuban = tbPackageMubanService.selectByPrimaryKey(String.valueOf(packageId));
                    Long packageMiejun = null;
                    if (packageMuban != null) {
                        packageMiejun = (long) packageMuban.getPackageMiejunFangshi();
                    }

                    Map<Long, TbDict> dictMap = StartUpOnLoadData.singleDataMap;
                    String packageMiejunFangshi = "";
                    if (dictMap.get(packageMiejun) != null) {
                        packageMiejunFangshi = dictMap.get(packageMiejun).getDictName();
                    }

                    if (!equipMiejunFangshi.equals(packageMiejunFangshi)) {
                        if (!miejunfangshis.contains(packageMiejunFangshi)) {
                            miejunfangshis.add(packageMiejunFangshi);
                        }
                    }
                }else{
                    rtnMap.put("state", "false");
                    rtnMap.put("msg", "有包不在待灭菌");
                    return rtnMap;
                }

            }

            ZsMiejunMingxi zsMiejunMingxi = null;
            for (int i = 0; i < baozhuangtiaomas.length; i++) {
                Map<String, Object> baoMap = new HashMap<>();
                baoMap.put("miejunWupinbaoTiaoma", baozhuangtiaomas[i]);
                zsMiejunMingxi = new ZsMiejunMingxi();
                String miejunmingxiliushui = sdf.format(new Date()) + seqVal[i];
                zsMiejunMingxi.setMiejunLiushui(miejunLiushui);
                zsMiejunMingxi.setMiejunMingxiLiushui(miejunmingxiliushui);
                zsMiejunMingxi.setMiejunHege(0);
                zsMiejunMingxi.setRecordTime(date);
                zsMiejunMingxi.setMiejunWupinbaoTiaoma(baozhuangtiaomas[i]);
                list1.add(zsMiejunMingxi);
                ZsPeibao peibao = new ZsPeibao();
                peibao.setPeibaoZhuangtai(2);
                peibao.setPeibaoBaozhuangTiaoma(baozhuangtiaomas[i]);
                peibaoList.add(peibao);
                ZsPackageImformation zsPackageImformation = new ZsPackageImformation();
                zsPackageImformation.setPackageLiuchengZhuangtai(5);
                zsPackageImformation.setMiejunLiushui(miejunLiushui);
                zsPackageImformation.setMiejunShebeiTiaoma(soft.getEquipId().toString());
                zsPackageImformation.setMiejunRuanjianTiaoma(miejunRuanjianTiaoma);
                zsPackageImformation.setMiejunHege(0);
                zsPackageImformation.setMiejunKaishiTime(date);
                zsPackageImformation.setMiejunZhuangtai(0);
                zsPackageImformation.setPackageTiaoma(baozhuangtiaomas[i]);
                if (tbEmployee != null) {
                    if (tbEmployee.getEmployeeNo().equals(miejunMeijunrenTiaoma)) {
                        zsPackageImformation.setMiejunMeijunrenTiaoma(miejunMeijunrenTiaoma);
                        packageImformations.add(zsPackageImformation);
                    }
                }
            }
            if (packageImformations.size() != 0) {
                if (miejunfangshis.size() >= 1) {
                    rtnMap.put("state", "false");
                    rtnMap.put("msg", "有不匹配的包");
                } else {
                    zsPackageInformationService.updateByBatch(packageImformations);
                    miejunService.insert(zsMiejun);
                    zsMiejunMingxiService.insertBatch(list1);
                    zsPeibaoService.updateBatchByBao(peibaoList);
                    rtnMap.put("state", "true");
                    rtnMap.put("msg", "开始成功");
                }
            } else {
                rtnMap.put("state", "false");
                rtnMap.put("msg", "无效灭菌人条码");
            }
        } catch (Exception e) {
            rtnMap.put("state", "false");
            rtnMap.put("msg", "开始失败");
            e.printStackTrace();
        }
        return rtnMap;
    }
}
